package com.geekbang;

import com.geekbang.supermarket.LittleSuperMarket;
import com.geekbang.supermarket.MerchandiseV2;
import com.geekbang.supermarket.ShellColorChangePhone;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ClassOfClassAppMain {


    public static void main(String... args) throws NoSuchFieldException, NoSuchMethodException {
        LittleSuperMarket superMarket = new LittleSuperMarket("大卖场",
                "世纪大道1号", 500, 600, 100);

        MerchandiseV2 m100 = superMarket.getMerchandiseOf(100);

        // >> TODO Class 描述类的类
        // >> TODO 由 类 或 类的对象 得到 Class 类的对象
        Class clazz = m100.getClass(); // m100.getClass() 等价于 ShellColorChangePhone.class;

        System.out.println(clazz.getName()); // com.geekbang.supermarket.ShellColorChangePhone
        System.out.println(clazz.getSimpleName());  // ShellColorChangePhone

        // TODO 通过一个Class类的实例，可以获取一个类所有的信息，包括成员变量，方法，等
//        Field countField = clazz.getField("count");
//        Field nameField = clazz.getField("count");
        Field countField = clazz.getField("count");
        System.out.println(countField.getType()); // int

        // >> TODO 变长参数和它的等价形式
//        Method equalsMethod = clazz.getMethod("equals", Object.class);
        Method buyMethod = clazz.getMethod("buy", int.class);
        Method equalsMethod = clazz.getMethod("equals", Object.class);

    }

    // TODO 变长参数
    public void dynaPara(int a, String... args) { // 语法糖，实际上就是个数组
        for(String s : args) {
            System.out.println(s);
        }
    }
}
